extends Node

func _ready():
	pass

func _init():
	call_deferred("init")

var Utils = null
var base = null
var Line = null

func init():
	if globalData.infoDs.has("g_ADDTALENT") || globalData.infoDs.has("g_quadruple_talent"):return

	if globalData.infoDs.has("g_ZHPBase"):
		base = globalData.infoDs["g_ZHPBase"]
	if base != null:
		#初始化
		Utils = globalData.infoDs["g_ZHPUtils"]
		Line = globalData.infoDs["g_ZHPLine"]
		base.connect("onLoadGame",self,"LoadGame")
		base.connect("onNewGame",self,"gameCtrl")
		base.loadSetting()
		Line.connect("resetData", self, "resetData")

func resetData():
	for i in talentSlot:
		i.queue_free()
	talentSlot.clear()
	readySlots.clear()

var talentSlot = []
var readySlots = []
func gameCtrl():
	var setting = base.getSetting("talentSetting",{"index":0,"size":8})
	if setting.size == 8:return
	for i in range(setting.size - 8):
		var t = preload("res://ui/talentBtn/talentBtn.tscn").instance()
		talentSlot.append(t)
		sys.main.get_node("ui/player/GridContainer").add_child(t)
	if not sys.main.player.is_connected("onPlusLv",self,"levelUp"):
		sys.main.player.connect("onPlusLv", self, "levelUp")

func LoadGame():
	gameCtrl()
	loadData()
	loadTalents()

func loadData():
	var setting = base.getSetting("talentSetting", {"index":0,"size":8})
	if setting.size == 8:return

	var diff = sys.main.player.lv - 8
	if diff <= 0:return
	for i in range(diff):
		if sys.main.player.talentDs.size() >= setting.size:break
		levelUp()

func loadTalents():
	var tmp = base.getData("talents", [])
	for i in tmp:
		var talent = talentData.infoDs.get(i)
		var t = load("%s/%s/%s.gd" % [talent.dir, talent.id, talent.id]).new()
		readySlots.pop_front().xueXi(t)

func levelUp():
	if sys.main.player.lv < 9 || talentSlot.size() == 0:return
	var t = talentSlot.pop_front()
	t.setStage(1)
	if sys.main.player.lv == 15:
		var t2 = talentSlot.pop_front()
		t2.setStage(1)
		readySlots.append(t2)